package com.always.electrical.energy.web.service;

import com.always.electrical.energy.config.Context;
import com.always.electrical.energy.utils.ChineseName;
import com.always.electrical.energy.web.req.DevicesControlMachineModeReq;
import com.always.electrical.energy.web.req.DevicesPowerFactorReq;
import com.always.electrical.energy.web.res.*;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * 假数据服务
 */
@Service
public class MockService {

    // 设备控制 模式 设置
    public void controlSetMachineMode(DeferredResult<WebBaseResponse> result, List<DevicesControlMachineModeReq.Item> items) {
        DevicesControlMachineModeRes webRes = new DevicesControlMachineModeRes();
        for (DevicesControlMachineModeReq.Item item : items) {
            DevicesControlMachineModeRes.Item res = new DevicesControlMachineModeRes.Item().setId(item.getId()).setMachineMode(item.getMachineMode());
            Context.devicesControlMachineModeRes.put(item.getId(), res);
            webRes.getItems().add(res);
        }
        result.setResult(WebBaseResponse.success(webRes));
    }

    // 目标功率因数 设置
    public WebResponse targetPowerFactorSet(List<DevicesPowerFactorReq.Item> items) {
        DevicesPowerFactorRes webRes = new DevicesPowerFactorRes();
        for (DevicesPowerFactorReq.Item item : items) {
            DevicesPowerFactorRes.Item res = new DevicesPowerFactorRes.Item().setId(item.getId()).setPowerFactor(item.getPowerFactor());
            Context.devicesPowerFactorRes.put(item.getId(), res);
            webRes.getItems().add(res);
        }
        return webRes;
    }

    // 电容器投切状态
    public DevicesCapacitorStateRes capacitorState(List<BigDecimal> deviceIds) {
        DevicesCapacitorStateRes webRes = new DevicesCapacitorStateRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesCapacitorStateRes.Item().setId(deviceId)
                    .setCapacitor1PitchCutA1("1").setCapacitor1PitchCutB1("1").setCapacitor1PitchCutC1("1")
                    .setCapacitor1PitchCutA2("0").setCapacitor1PitchCutB2("0").setCapacitor1PitchCutC2("1")
                    .setCapacitor2PitchCutC1("0").setCapacitor2PitchCutC2("1").setCapacitor2PitchCutC3("0")
                    .setCapacitor3PitchCutC1("1").setCapacitor3PitchCutC2("1").setCapacitor3PitchCutC3("1")
            );
        }
        return webRes;
    }

    // 显示数据数据项
    public DevicesSvgCurveDataRes svgCurveData(List<BigDecimal> deviceIds) {
        DevicesSvgCurveDataRes webRes = new DevicesSvgCurveDataRes();

        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesSvgCurveDataRes.Item().setId(deviceId)

                    // 功率因数 电网侧
                    .setTotPF(BigDecimal.valueOf(2))
                    .setPhPF_phsA(BigDecimal.valueOf(3))
                    .setPhPF_phsB(BigDecimal.valueOf(4))
                    .setPhPF_phsC(BigDecimal.valueOf(5))

                    // 功率因数 负载侧
                    .setLoad_TotPF(BigDecimal.valueOf(6))
                    .setLoad_PhPF_phsA(BigDecimal.valueOf(7))
                    .setLoad_PhPF_phsB(BigDecimal.valueOf(8))
                    .setLoad_PhPF_phsC(BigDecimal.valueOf(9))

                    // 无功功率 电网侧
                    .setGrid_TotVAr(BigDecimal.valueOf(2))
                    .setGrid_PhVAr_phsA(BigDecimal.valueOf(3))
                    .setGrid_PhVAr_phsB(BigDecimal.valueOf(4))
                    .setGrid_PhVAr_phsC(BigDecimal.valueOf(5))

                    // 无功功率 负载侧
                    .setLoad_TotVAr(BigDecimal.valueOf(6))
                    .setLoad_PhVAr_phsA(BigDecimal.valueOf(7))
                    .setLoad_PhVAr_phsB(BigDecimal.valueOf(8))
                    .setLoad_PhVAr_phsC(BigDecimal.valueOf(9))

                    // THD 电流 电网侧
                    .setGrid_THD_A_phsA(BigDecimal.valueOf(2))
                    .setGrid_THD_A_phsB(BigDecimal.valueOf(3))
                    .setGrid_THD_A_phsC(BigDecimal.valueOf(4))

                    // THD 电流 负载侧
                    .setLoad_THD_A_phsA(BigDecimal.valueOf(5))
                    .setLoad_THD_A_phsB(BigDecimal.valueOf(6))
                    .setLoad_THD_A_phsC(BigDecimal.valueOf(7))
                    .setLoad_THD_A_phsN(BigDecimal.valueOf(8))

                    // THD 电压 电网侧
                    .setGrid_THD_PhV_phsA(BigDecimal.valueOf(2))
                    .setGrid_THD_PhV_phsB(BigDecimal.valueOf(3))
                    .setGrid_THD_PhV_phsC(BigDecimal.valueOf(4))

                    // 电流不平衡度 电网侧
                    .setGrid_A_Unbalance(BigDecimal.valueOf(2))
                    .setGrid_A_Negative_Unbalance(BigDecimal.valueOf(3))
                    .setGrid_A_Zero_Unbalance(BigDecimal.valueOf(4))

                    // 电流不平衡度 负载侧
                    .setLoad_A_Unbalance(BigDecimal.valueOf(5))
                    .setLoad_A_Negative_Unbalance(BigDecimal.valueOf(6))
                    .setLoad_A_Zero_Unbalance(BigDecimal.valueOf(7))

                    // 视在功率 电网侧
                    .setGrid_TotVA(BigDecimal.valueOf(2))
                    .setGrid_PhVA_phsA(BigDecimal.valueOf(3))
                    .setGrid_PhVA_phsB(BigDecimal.valueOf(4))
                    .setGrid_PhVA_phsC(BigDecimal.valueOf(5))

                    // 视在功率 负载侧
                    .setLoad_TotVA(BigDecimal.valueOf(6))
                    .setLoad_PhVA_phsA(BigDecimal.valueOf(7))
                    .setLoad_PhVA_phsB(BigDecimal.valueOf(8))
                    .setLoad_PhVA_phsC(BigDecimal.valueOf(9))

                    // 电流 电网侧
                    .setGrid_A_phsA(BigDecimal.valueOf(2))
                    .setGrid_A_phsB(BigDecimal.valueOf(3))
                    .setGrid_A_phsC(BigDecimal.valueOf(4))
                    .setGrid_A_phsN(BigDecimal.valueOf(5))

                    // 电流 电网侧
                    .setLoad_A_phsA(BigDecimal.valueOf(6))
                    .setLoad_A_phsB(BigDecimal.valueOf(7))
                    .setLoad_A_phsC(BigDecimal.valueOf(8))
                    .setLoad_A_phsN(BigDecimal.valueOf(9))
            );
        }
        return webRes;
    }

    // 变压器 查询
    public WebResponse transformer(List<BigDecimal> deviceIds) {
        DevicesTransformerRes webRes = new DevicesTransformerRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesTransformerRes.Item().setId(deviceId).setV(BigDecimal.valueOf(220.1)).setA(BigDecimal.valueOf(110.5))
                    .setW(BigDecimal.valueOf(600.3)).setWRate(BigDecimal.valueOf(13.1)));
        }
        return webRes;
    }

    // 融合终端 查询
    public WebResponse convergedTerminals(List<BigDecimal> deviceIds) {
        DevicesConvergedTerminalsRes webRes = new DevicesConvergedTerminalsRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesConvergedTerminalsRes.Item().setId(deviceId).setV(BigDecimal.valueOf(220.1)).setA(BigDecimal.valueOf(110.5))
                    .setW(BigDecimal.valueOf(600.3)).setWRate(BigDecimal.valueOf(13.1)));
        }
        return webRes;
    }

    // 分支箱 查询
    public WebResponse branchBoxes(List<BigDecimal> deviceIds) {
        DevicesBranchBoxesRes webRes = new DevicesBranchBoxesRes();
        for (BigDecimal deviceId : deviceIds) {
//            webRes.getItems().add(new DevicesBranchBoxesRes.Item().setId(deviceId).setV(BigDecimal.valueOf(220.1)).setA(BigDecimal.valueOf(110.5))
//                    .setW(BigDecimal.valueOf(600.3)).setWRate(BigDecimal.valueOf(13.1)));
        }
        return webRes;
    }

    // 电线杆 查询
    public WebResponse pole(List<BigDecimal> deviceIds) {
        DevicesPoleRes webRes = new DevicesPoleRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesPoleRes.Item().setId(deviceId).setPositiveA(BigDecimal.valueOf(220)).setNegativeA(BigDecimal.valueOf(-110)));
        }
        return webRes;
    }

    // PCS 交流侧电压 查询
    public void pcsAc(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        MicrogridPcsAcRes webRes = new MicrogridPcsAcRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new MicrogridPcsAcRes.Item().setId(deviceId).setAc(BigDecimal.valueOf(220)));
        }
        result.setResult(WebBaseResponse.success(webRes));
    }

    // 储能 可充放电状态 查询
    public void chargeState(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        MicrogridChargeStateRes webRes = new MicrogridChargeStateRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new MicrogridChargeStateRes.Item().setId(deviceId).setState(1));
        }
        result.setResult(WebBaseResponse.success(webRes));
    }

    /*
    // 充电中心（充电桩）
    public MscChargeRes charge(List<BigDecimal> deviceIds) {
        MscChargeRes webRes = new MscChargeRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new MscChargeRes.Item().setId(deviceId).setState(1)
                    .setYesterdayCharge(BigDecimal.valueOf(2)).setCumulativeCharge(BigDecimal.valueOf(3)).setYesterdayChargeNum(4)
                    .setCumulativeChargeNum(5).setDailyCharge(BigDecimal.valueOf(6))
                    .setW(BigDecimal.valueOf(301)).setV(BigDecimal.valueOf(221)).setA(BigDecimal.valueOf(31)));
        }
        return webRes;
    }
    */

    /*
    // 光伏（MPPT)
    public MscMpptRes mppt(List<BigDecimal> deviceIds) {
        MscMpptRes webRes = new MscMpptRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new MscMpptRes.Item().setId(deviceId).setState(1).setYesterdayElect(BigDecimal.valueOf(2))
                    .setCumulativeElect(BigDecimal.valueOf(2)).setDailyElect(BigDecimal.valueOf(3))
                    .setW(BigDecimal.valueOf(400)).setV(BigDecimal.valueOf(223)).setA(BigDecimal.valueOf(50)));
        }
        return webRes;
    }
    */

    // 电表
    public WebResponse meter(List<BigDecimal> deviceIds) {
        DevicesMeterRes webRes = new DevicesMeterRes();
        for (BigDecimal deviceId : deviceIds) {
            webRes.getItems().add(new DevicesMeterRes.Item().setId(deviceId)
                    .setAccountName(ChineseName.randomName())
                    .setAccountNum(BigDecimal.valueOf(new Random().nextInt(500)))
                    .setMeterNum(BigDecimal.valueOf(new Random().nextInt(500))));
        }
        return webRes;
    }
}
